"""
    连接数据库的步骤
    1. 导入需要的包
        1. mysql-connector
        2. mysql-connector-python
    2. 配置连接

"""
import mysql.connector

con = None
cursor = None

try:
    con = mysql.connector.connect(
        host='127.0.0.1',
        user='root',
        password='123456',
        port='3306',
        charset='utf8',
        database='examtest'

    )
except mysql.connector.Error:
    print('数据库连接失败')
else:
    print('连接成功--正在查询数据........')
    sql = "select * from students"
    cursor = con.cursor()
    cursor.execute(sql)
    values = cursor.fetchall()
    for data in values:
        print(data)


finally:
    cursor.close()
    con.close()
